package com.tju.resource.business.tb_app_log.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.business.tb_app_log.TbAppLogService;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.SysUserMapper;
import com.tju.resource.mapper.TbAppLogMapper;
import com.tju.resource.model.SysUser;
import com.tju.resource.model.TbAppLog;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 审核记录
 */
@Service
public class TbAppLogServiceImpl extends ServiceImpl<TbAppLogMapper, TbAppLog>
        implements TbAppLogService {
    @Autowired
    private TbAppLogMapper tbAppLogMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 新增审核记录
     *
     * @param tbAppLog 审核记录内容
     * @return
     */
    @Override
    public boolean insertTbAppLog(TbAppLog tbAppLog) {
        if (EmptyUtils.isEmpty(tbAppLog.getUserName()))
            throw new ServiceException("用户名不能为空");
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("username", tbAppLog.getUserName());
        SysUser sysUser = sysUserMapper.selectOne(qw);
        if (EmptyUtils.isEmpty(sysUser))
            throw new ServiceException("用户名不存在");
        tbAppLog.setAppById(sysUser.getId());//用户id
        return tbAppLogMapper.insert(tbAppLog) > 0;
    }

    /**
     * 根据出货id查询审核记录
     *
     * @param map 出货id
     * @return 结果
     */
    @Override
    public Result<Object> selectTbAppLogByShipmentId(Map<String, String> map) {
        try {
            if (EmptyUtils.isEmpty(map.get("id")))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "出货id不能为空");
            return ResultUtil.success(ResultEnum.SUCCESS, tbAppLogMapper.selectTbAppLogByShipmentId(Long.parseLong(map.get("id"))));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 根据外发id查询审核记录
     *
     * @param map 外发id
     * @return 结果
     */
    @Override
    public Result<Object> selectTbAppLogByOutwardId(Map<String, String> map) {
        try {
            if (EmptyUtils.isEmpty(map.get("id")))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "外发id不能为空");
            return ResultUtil.success(ResultEnum.SUCCESS, tbAppLogMapper.selectTbAppLogByOutwardId(Long.parseLong(map.get("id"))));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 根据施工单id查询审核记录
     *
     * @param map 施工单id
     * @return 结果
     */
    @Override
    public Result<Object> selectTbAppLogByImplOrderId(Map<String, String> map) {
        try {
            if (EmptyUtils.isEmpty(map.get("id")))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "施工单id不能为空");
            return ResultUtil.success(ResultEnum.SUCCESS, tbAppLogMapper.selectTbAppLogByImplOrderId(Long.parseLong(map.get("id"))));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 根据采购id查询审核记录
     *
     * @param map 采购id
     * @return 结果
     */
    @Override
    public Result<Object> selectTbAppLogByPurchaseId(Map<String, String> map) {
        try {
            if (EmptyUtils.isEmpty(map.get("id")))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "采购id不能为空");
            return ResultUtil.success(ResultEnum.SUCCESS, tbAppLogMapper.selectTbAppLogByPurchaseId(Long.parseLong(map.get("id"))));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }
}




